package com.app.service.impl;

import com.app.db.datasource.M1DataBaseContextHolder;
import com.app.db.datasource.M2DataBaseContextHolder;
import com.app.db.mybatis.m1.domain.User1;
import com.app.db.mybatis.m1.mapper.User1Mapper;
import com.app.db.mybatis.m2.domain.User2;
import com.app.db.mybatis.m2.mapper.User2Mapper;
import com.app.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private User1Mapper user1Mapper;
    @Resource
    private User2Mapper user2Mapper;


    @Override
    @Transactional
    public void add() {
        User1 user= new User1();
        user.setIduser(System.currentTimeMillis());
        user.setUserName(System.currentTimeMillis()+"master1");
        user.setUserAge(1);
        user.setUserStatus(new Byte("0"));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num1= user1Mapper.insert(user);

        System.out.println("==M1==>"+num1);
        User2 user2= new User2();
        user2.setIduser(System.currentTimeMillis());
        user2.setUserName(System.currentTimeMillis()+"master2");
        user2.setUserAge(20);
        user2.setUserStatus(new Byte("0"));
        user2.setCreateTime(new Date());
        user2.setUpdateTime(new Date());
        int num2= user2Mapper.insert(user2);
        System.out.println("==M2==>"+num2);

    }

    @Override
    public void select() {

        User1 user= new User1();
        user.setIduser(System.currentTimeMillis());
        user.setUserName(System.currentTimeMillis()+"master1");
        user.setUserAge(1);
        user.setUserStatus(new Byte("0"));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num1= user1Mapper.insert(user);
        System.out.println("==M1==>"+num1);

        M1DataBaseContextHolder.setSalve();
        System.out.println("==S1=="+ user1Mapper.selectByPrimaryKey(1L).getUserName());

        User2 user2= new User2();
        user2.setIduser(System.currentTimeMillis());
        user2.setUserName(System.currentTimeMillis()+"master2");
        user2.setUserAge(20);
        user2.setUserStatus(new Byte("0"));
        user2.setCreateTime(new Date());
        user2.setUpdateTime(new Date());
        int num2= user2Mapper.insert(user2);
        System.out.println("==M2==>"+num2);

        M2DataBaseContextHolder.setSalve();
        System.out.println("==s2=="+ user2Mapper.selectByPrimaryKey(2L).getUserName());


    }
}
